package com.example.pro1;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.View;
import android.widget.TextView;

//篮球积分程序2.0版横屏版
public class BasketballScoreActivity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_basketball_score2);
    }


    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        TextView TeamA = findViewById(R.id.scoreLt), TeamB = findViewById(R.id.scoreRt);

        //获取控件的信息并写入bundle，可以把bundle理解为一个压缩包
        String teama = TeamA.getText().toString(), teamb = TeamB.getText().toString();
        outState.putString("TeamA", teama);
        outState.putString("TeamB", teamb);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        TextView TeamA = findViewById(R.id.scoreLt), TeamB = findViewById(R.id.scoreRt);

        //从budle中获取数据并写入数据
        String teama = savedInstanceState.getString("TeamA");
        String teamb = savedInstanceState.getString("TeamB");
        TeamA.setText(teama);
        TeamB.setText(teamb);
    }

    public void scorebtu(View v) {
        //获取发出请求事件的id值和两队分数的控件
        int id = v.getId();
        TextView TeamA = findViewById(R.id.scoreLt), TeamB = findViewById(R.id.scoreRt);

        //如果是同时清零，直接使用
        if(id == R.id.Reset) {
            TeamA.setText("0");
            TeamB.setText("0");
        }
        else {
            //根据不同情况进行设定
            int STA = Integer.parseInt(TeamA.getText().toString()), STB = Integer.parseInt(TeamB.getText().toString());
            if(id == R.id.scoreLt3) TeamA.setText(String.valueOf(STA+3));
            else if(id == R.id.scoreLt2) TeamA.setText(String.valueOf(STA+2));
            else if(id == R.id.FreeLt) TeamA.setText(String.valueOf(0));
            else if(id == R.id.scoreRt3) TeamB.setText(String.valueOf(STB+3));
            else if(id == R.id.scoreRt2) TeamB.setText(String.valueOf(STB+2));
            else if(id == R.id.FreeRt) TeamB.setText(String.valueOf(0));
        }
    }
}